package com.yxjx.duoxue.comment;

import java.util.Properties;

import com.tencent.stat.StatService;
import com.yxjx.duoxue.R;
import com.yxjx.duoxue.Yx;
import com.yxjx.duoxue.course.CourseIntroductionActivity;
import com.yxjx.duoxue.course.SchoolIntroductionActivity;
import com.yxjx.duoxue.datamodel.DMComment;
import com.yxjx.duoxue.datamodel.course.DMCourse;
import com.yxjx.duoxue.datamodel.school.DMSchool;
import com.yxjx.duoxue.mta.stat.StatHelper;
import com.yxjx.duoxue.my.UserLoginActivity;
import com.yxjx.duoxue.network.UserLoginResponse;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceManager.OnActivityResultListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class CommentFunctionView extends LinearLayout implements OnClickListener {
	
	public static final String KEY_COMMENT_TYPE = "KEY_COMMENT_TYPE";
	public static final String KEY_ITEM_ID = "KEY_ITEM_ID";

	public static final int REQ_ADD_COMMENT = 1;
	private Activity mHost;
	private int mCommentType = -1;
	private String mItemId = "";
	private CommentItemView mHotCommentItem;
	private TextView addText;
	private int mCommentNum = 0;
	private View mHotCommentItemDivider;

	public CommentFunctionView(Context context, AttributeSet attrs) {
		super(context, attrs);
		View root = LayoutInflater.from(context).inflate(R.layout.segment_comment_function, this, true);
		
		UIHelper.setText(root.findViewById(R.id.comment_title), R.id.text, "家长点评");
		mHost = (Activity) getContext();
		Utils.logd("getContext() = " + getContext());
 		UIHelper.setClickListener(root, R.id.add_comment, this);
		UIHelper.setClickListener(root, R.id.view_comment, this);
		UIHelper.setClickListener(root, R.id.commentItemView, this);
		
		addText = (TextView) findViewById(R.id.add_comment);
		
		mHotCommentItem = (CommentItemView) root.findViewById(R.id.commentItemView);
		mHotCommentItemDivider = root.findViewById(R.id.commentItemView_divider);
		mHotCommentItem.setAsCommentFunctionMode();
		
	}
	
	public void init(DMComment comment) {
		mHotCommentItem.setComment(comment);
		mHotCommentItem.setAsCommentFunctionMode();
	}
	
	public void initAsNoComment() {
		mHotCommentItem.setVisibility(View.GONE);
		mHotCommentItemDivider.setVisibility(View.GONE);
		addText.setText("上传第一条点评");
		addText.setTextColor(getResources().getColor(R.color.yx_green));
	}
	
	public void setNum(int num) {
		mCommentNum = num;
		if (mHost instanceof CourseIntroductionActivity) {
			((TextView) findViewById(R.id.comments_num)).setText("查看全部点评（" + num + "）");
			if (num == 0) {
				findViewById(R.id.add_comment).setVisibility(View.VISIBLE);
				findViewById(R.id.view_comment).setVisibility(View.INVISIBLE);
			} else {
				findViewById(R.id.add_comment).setVisibility(View.INVISIBLE);
				findViewById(R.id.view_comment).setVisibility(View.VISIBLE);
			}
		} else if (mHost instanceof SchoolIntroductionActivity) {
			((TextView) findViewById(R.id.comments_num)).setText("查看全部点评（" + num + "）");
			if (num == 0) {
				this.setVisibility(View.GONE);
			} else {
				this.setVisibility(View.VISIBLE);
			}
		}
	}
	
	@Override
	public void onClick(View view) {
		
		if (mHost instanceof CourseIntroductionActivity) {
			String courseId = ((CourseIntroductionActivity) mHost).getCurCourseId();
			mCommentType = Yx.TYPE_ITEM_COURSE;
			mItemId = "" + courseId;
		} else if (mHost instanceof SchoolIntroductionActivity) {
			String schoolId = ((SchoolIntroductionActivity) mHost).getCurSchoolId();
			mCommentType = Yx.TYPE_ITEM_SCHOOL;
			mItemId = "" + schoolId;
		}
		
		Intent intent;
		switch (view.getId()) {
		case R.id.add_comment:
			UserLoginResponse ulRsp = UserLoginResponse.getInstance(mHost);
			if (ulRsp == null) {
				if (mHost instanceof CourseIntroductionActivity) {
					mHost.startActivityForResult(
							new Intent(mHost, UserLoginActivity.class),
							CourseIntroductionActivity.REQ_CODE_LOGIN_FOR_ADD_COMMENT);
				} else if (mHost instanceof SchoolIntroductionActivity) {
					mHost.startActivityForResult(
							new Intent(mHost, UserLoginActivity.class),
							SchoolIntroductionActivity.REQ_CODE_LOGIN_FOR_ADD_COMMENT);
				}
			} else {
				addingComment();
			}
			break;
		case R.id.view_comment:
		case R.id.commentItemView:
			if (mCommentNum <= 0) {
				break;
			}
			intent = new Intent(mHost, DetailedCommentActivity.class);
			intent.putExtra(KEY_COMMENT_TYPE, mCommentType);
			intent.putExtra(KEY_ITEM_ID, mItemId);
			
			/// MTA stat start
			Properties prop = StatHelper.getBasicProp(getContext());
			if (mCommentType == Yx.TYPE_ITEM_SCHOOL) {
				prop.put(StatHelper.KEY_SCHOOL, mItemId);
				StatService.trackCustomKVEvent(getContext(), "event_school_comment_view", prop);
			} else  {
				prop.put(StatHelper.KEY_COURSE, mItemId);
				StatService.trackCustomKVEvent(getContext(), "event_course_comment_view", prop);
			}
			/// MTA stat end
			mHost.startActivity(intent);
			break;
		default:
			break;
		}
		
	}

	/**
	 * public to be called in Host Activity.
	 */
	public void addingComment() {
		Intent intent;
		intent = new Intent(mHost, AddingCommentsActivity.class);
		intent.putExtra(KEY_COMMENT_TYPE, mCommentType);
		intent.putExtra(KEY_ITEM_ID, mItemId);
		mHost.startActivityForResult(intent, REQ_ADD_COMMENT);
	}
	
}
